import pdf from 'vue-pdf'
import {uploadContractFile} from '../api/rent-contract.js'
import {putObj,addObj} from "../api/rent-contract";
import {mapState} from "vuex";

export default {
    name: "contract-ocr-add",
    components: {pdf},
    data() {
        return {
            dialogVisible: false,
            contractFile: null,
            pdfUrl: "",
            contractUrl:"",

            //此处有问题，后续没地方改类型
            type: "02",
            subType: "1",

            freeFees: [],//免租期，日期范围需要监听，赋值到formdata的两个值去

            dialogContractVisible: false,//合同放大查看

            formData: {

                //contractTypes: [],
                //contractSubTypes:[],
            },

            rentFeeInformation: [],

        }
    },

    mounted: function () {

    },

    computed: mapState({
            //收费方式
            rentFeeType: state => {
                let data = state.dict.find(item => item.type === "rent_fee_type") || {};
                return data.children|| [];
            }
    }
    ),

    methods: {

        show() {
            this.dialogVisible = true;
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
        },

        // 识别文件
        getFile(event) {
            this.contractFile = event.target.files[0];
        },

        //上传附件
        upload() {
            if (this.contractFile == null) {
                this.$alert("请先选择文件，再上传");
                return;
            }

            let formData = new FormData();
            formData.append('file', this.contractFile);

            uploadContractFile(formData).then(response => {
                if (response.data.code == 0) {
                    this.$alert("上传附件成功");
                    //this.$alert(window.location.origin);
                    //this.$alert(response.data.data.url);
                    this.pdfUrl = window.location.origin + response.data.data.url;

                    this.contractUrl = response.data.data.url;
                    //alert(this.formData.pdfUrl);
                    //this.formData.contractName=this.contractName;

                } else {
                    this.$alert("服务端异常：" + response.message);
                }
            })
        },

        read() {

            //目前写死
            this.formData = {
                customerId:'09a5a8aeda8847e98c1ae4a11d18b93d',
                customerName:"华为技术有限公司",
                clientLesseeName: "华为技术有限公司",
                certificateType: "2",
                certificateNum: "911310016636709451",
                address: "广东省广州市荔湾区天河",
                telephone: "15238288888",


                propertyNames: "华富村一期1栋101",
                buildingArea: 120,
                rentAmount: 12000,
                feeType:'2',
                startDate: '2020-10-01',
                endDate: '2021-12-31',

                rentStartDate: '2020-10-16',
                proFeeAmount: '24000',
                category: 1


            };
            this.freeFees = ['2020-10-01', '2020-10-15']

            //费用区间
            this.rentFeeInformation =
                [
                    {
                        startDate: '2020-10-16',
                        endDate: '2020-12-31',
                        priceFinal: 12000,
                        remarks: ''

                    },
                    {
                        startDate: '2021-01-01',
                        endDate: '2021-12-31',
                        priceFinal: 13200,
                        remarks: '调价10%'
                    }
                ];
        },


        ok() {
            //先新增
            if (this.contractType != '' && this.contractDetail != '') {


                addObj(Object.assign({type: this.type, subType: this.subType}))
                    .then(response => {
                        if (response.data.code == 0) {
                            //alert(JSON.stringify(response));
                            let id = response.data.data;
                            this.formData.rentContractId = id;

                            //alert(JSON.stringify(this.formData));
                            //如果新增成功，修改相关信息
                            putObj(Object.assign(this.formData,{contractUrl:this.contractUrl},
                                {
                                    rentContractDetails: this.rentFeeInformation,
                                },
                                {
                                    rentContractProfees: [{feeitemId:'1',chargeProjectName:'租赁押金',amount:this.formData.proFeeAmount}]
                                },
                                {
                                    rentContractRooms: [{roomId:'e46170823a5a8a417f8ef8e47ed0c3d8',propertyName:'华富村一期1栋101',buildingArea: 200,price:78}]
                                }))
                                .then(response => {
                                        if (response.data.code == 0) {
                                            this.$alert("提交成功");

                                            //跳转到编辑页面
                                            this.$router.push({path: "/wy-rent/contract/edit?id=" + id });

                                        } else {
                                            this.$alert("保存信息提交失败" + response.data.msg);
                                        }
                                    }
                                )
                        }
                        else
                            {
                                this.$alert("新增合同服务器返回错误" + response.code + response.message);
                            }
                        }
                    )
            } else {
                this.$alert("请选择合同模板和合同细类")
            }

        }

    }

}
